﻿using System;
using System.Collections.Generic;
using System.Text;
using RimWorld;
using UnityEngine;
using Verse;

namespace RI_Living
{
	// Token: 0x02000002 RID: 2
	[StaticConstructorOnStartup]
	public class Building_Fermentation : Building
	{
		public float Progress
		{
			get
			{
				return this.progressInt;
			}
			set
			{
				bool flag = value != this.progressInt;
				if (flag)
				{
					this.progressInt = value;
					this.barFilledCachedMat = null;
				}
			}
		}

		// Token: 0x17000002 RID: 2
		// (get) Token: 0x06000003 RID: 3 RVA: 0x00002098 File Offset: 0x00000298
		private Material BarFilledMat
		{
			get
			{
				bool flag = this.barFilledCachedMat == null;
				if (flag)
				{
					this.barFilledCachedMat = SolidColorMaterials.SimpleSolidColorMaterial(Color.Lerp(Building_Fermentation.BarZeroProgressColor, Building_Fermentation.BarFermentedColor, this.Progress), false);
				}
				return this.barFilledCachedMat;
			}
		}

		// Token: 0x17000003 RID: 3
		// (get) Token: 0x06000004 RID: 4 RVA: 0x000020E4 File Offset: 0x000002E4
		public int SpaceLeftForWort
		{
			get
			{
				bool flag = !this.Fermented;
				int result;
				if (flag)
				{
					result = this.def.GetModExtension<Building_FermentationDefMod>().MaxCapacity - this.wortCount;
				}
				else
				{
					result = 0;
				}
				return result;
			}
		}

		// Token: 0x17000004 RID: 4
		// (get) Token: 0x06000005 RID: 5 RVA: 0x0000211F File Offset: 0x0000031F
		private bool Empty
		{
			get
			{
				return this.wortCount <= 0;
			}
		}

		// Token: 0x17000005 RID: 5
		// (get) Token: 0x06000006 RID: 6 RVA: 0x00002130 File Offset: 0x00000330
		public bool Fermented
		{
			get
			{
				bool flag = !this.Empty;
				return flag && this.Progress >= 1f;
			}
		}

		// Token: 0x17000006 RID: 6
		// (get) Token: 0x06000007 RID: 7 RVA: 0x00002164 File Offset: 0x00000364
		private float CurrentTempProgressSpeedFactor
		{
			get
			{
				CompProperties_TemperatureRuinable compProperties = this.def.GetCompProperties<CompProperties_TemperatureRuinable>();
				float ambientTemperature = base.AmbientTemperature;
				bool flag = ambientTemperature < compProperties.minSafeTemperature;
				float result;
				if (flag)
				{
					result = 0.1f;
				}
				else
				{
					bool flag2 = ambientTemperature < (float)this.def.GetModExtension<Building_FermentationDefMod>().MinIdealTemperature;
					if (flag2)
					{
						result = GenMath.LerpDouble(compProperties.minSafeTemperature, (float)this.def.GetModExtension<Building_FermentationDefMod>().MinIdealTemperature, 0.1f, 1f, ambientTemperature);
					}
					else
					{
						result = 1f;
					}
				}
				return result;
			}
		}

		// Token: 0x17000007 RID: 7
		// (get) Token: 0x06000008 RID: 8 RVA: 0x000021E9 File Offset: 0x000003E9
		private float ProgressPerTickAtCurrentTemp
		{
			get
			{
				return 2.7777778E-06f * this.CurrentTempProgressSpeedFactor;
			}
		}

		// Token: 0x17000008 RID: 8
		// (get) Token: 0x06000009 RID: 9 RVA: 0x000021F7 File Offset: 0x000003F7
		private int EstimatedTicksLeft
		{
			get
			{
				return Mathf.Max(Mathf.RoundToInt((1f - this.Progress) / this.ProgressPerTickAtCurrentTemp), 0);
			}
		}

		// Token: 0x0600000A RID: 10 RVA: 0x00002217 File Offset: 0x00000417
		public override void ExposeData()
		{
			base.ExposeData();
			Scribe_Values.Look<int>(ref this.wortCount, "wortCount", 0, false);
			Scribe_Values.Look<float>(ref this.progressInt, "progress", 0f, false);
		}

		// Token: 0x0600000B RID: 11 RVA: 0x0000224C File Offset: 0x0000044C
		public override void TickRare()
		{
			base.TickRare();
			bool flag = !this.Empty;
			if (flag)
			{
				this.Progress = Mathf.Min(this.Progress + 250f * this.ProgressPerTickAtCurrentTemp, 1f);
			}
		}

		// Token: 0x0600000C RID: 12 RVA: 0x00002294 File Offset: 0x00000494
		public void AddWort(int count)
		{
			base.GetComp<CompTemperatureRuinable>().Reset();
			bool fermented = this.Fermented;
			if (fermented)
			{
				Log.Warning("Tried to add wort to a barrel full of beer. Colonists should take the beer first.");
			}
			else
			{
				int num = Mathf.Min(count, this.def.GetModExtension<Building_FermentationDefMod>().MaxCapacity - this.wortCount);
				bool flag = num > 0;
				if (flag)
				{
					this.Progress = GenMath.WeightedAverage(0f, (float)num, this.Progress, (float)this.wortCount);
					this.wortCount += num;
				}
			}
		}

		// Token: 0x0600000D RID: 13 RVA: 0x0000231C File Offset: 0x0000051C
		protected override void ReceiveCompSignal(string signal)
		{
			bool flag = signal == "RuinedByTemperature";
			if (flag)
			{
				this.Reset();
			}
		}

		// Token: 0x0600000E RID: 14 RVA: 0x00002342 File Offset: 0x00000542
		private void Reset()
		{
			this.wortCount = 0;
			this.Progress = 0f;
		}

		// Token: 0x0600000F RID: 15 RVA: 0x00002358 File Offset: 0x00000558
		public void AddWort(Thing wort)
		{
			int num = Mathf.Min(wort.stackCount, this.def.GetModExtension<Building_FermentationDefMod>().MaxCapacity - this.wortCount);
			bool flag = num > 0;
			if (flag)
			{
				this.AddWort(num);
				wort.SplitOff(num).Destroy(DestroyMode.Vanish);
			}
		}

		// Token: 0x06000010 RID: 16 RVA: 0x000023AC File Offset: 0x000005AC
		public override string GetInspectString()
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append(base.GetInspectString());
			bool flag = stringBuilder.Length != 0;
			if (flag)
			{
				stringBuilder.AppendLine();
			}
			CompTemperatureRuinable comp = base.GetComp<CompTemperatureRuinable>();
			bool flag2 = !this.Empty && !comp.Ruined;
			if (flag2)
			{
				bool fermented = this.Fermented;
				if (fermented)
				{
					stringBuilder.AppendLine("RI_Living_Harvest".Translate(this.wortCount, this.def.GetModExtension<Building_FermentationDefMod>().MaxCapacity));
				}
				else
				{
					stringBuilder.AppendLine("RI_Living_Fill".Translate(this.wortCount, this.def.GetModExtension<Building_FermentationDefMod>().MaxCapacity));
				}
			}
			bool flag3 = !this.Empty;
			if (flag3)
			{
				bool fermented2 = this.Fermented;
				if (fermented2)
				{
					stringBuilder.AppendLine("RI_Living_Fermented".Translate());
				}
				else
				{
					stringBuilder.AppendLine("RI_Living_FermentationProgress".Translate(this.Progress.ToStringPercent(), this.EstimatedTicksLeft.ToStringTicksToPeriod(true, false, true, true, false)));
					bool flag4 = this.CurrentTempProgressSpeedFactor != 1f;
					if (flag4)
					{
						stringBuilder.AppendLine("RI_Living_FermentationBarrelOutOfIdealTemperature".Translate(this.CurrentTempProgressSpeedFactor.ToStringPercent()));
					}
				}
			}
			stringBuilder.AppendLine("Temperature".Translate() + ": " + base.AmbientTemperature.ToStringTemperature("F0"));
			stringBuilder.AppendLine("RI_Living_IdealFermentingTemperature".Translate() + ": " + comp.Props.minSafeTemperature.ToStringTemperature("F0") + " ~ " + comp.Props.maxSafeTemperature.ToStringTemperature("F0"));
			return stringBuilder.ToString().TrimEndNewlines();
		}

		// Token: 0x06000011 RID: 17 RVA: 0x000025E0 File Offset: 0x000007E0
		public Thing TakeOutBeer()
		{
			bool flag = !this.Fermented;
			Thing result;
			if (flag)
			{
				Log.Warning("Tried to get thing but it's not yet fermented.");
				result = null;
			}
			else
			{
				Thing thing = ThingMaker.MakeThing(this.def.GetModExtension<Building_FermentationDefMod>().GetThingDef, null);
				float num = (float)this.wortCount / this.def.GetModExtension<Building_FermentationDefMod>().Proportion;
				int b = (int)num;
				int stackCount = Mathf.Max(1, b);
				thing.stackCount = stackCount;
				this.Reset();
				result = thing;
			}
			return result;
		}

		// Token: 0x06000012 RID: 18 RVA: 0x00002660 File Offset: 0x00000860
		protected override void DrawAt(Vector3 drawLoc, bool flip = false)
		{
			base.DrawAt(drawLoc, flip);
			bool flag = !this.Empty;
			if (flag)
			{
				Vector3 center = drawLoc;
				center.y += 0.03846154f;
				center.z += 0.25f;
				GenDraw.DrawFillableBar(new GenDraw.FillableBarRequest
				{
					center = center,
					size = Building_Fermentation.BarSize,
					fillPercent = (float)this.wortCount / (float)this.def.GetModExtension<Building_FermentationDefMod>().MaxCapacity,
					filledMat = this.BarFilledMat,
					unfilledMat = Building_Fermentation.BarUnfilledMat,
					margin = 0.1f,
					rotation = Rot4.North
				});
			}
		}

		// Token: 0x06000013 RID: 19 RVA: 0x0000271F File Offset: 0x0000091F
		public override IEnumerable<Gizmo> GetGizmos()
		{
			foreach (Gizmo gizmo in base.GetGizmos())
			{
				yield return gizmo;
			}
			IEnumerator<Gizmo> enumerator = null;
			bool flag = !DebugSettings.ShowDevGizmos;
			if (flag)
			{
				yield break;
			}
			bool flag2 = !this.Empty;
			if (flag2)
			{
				Command_Action command_Action = new Command_Action();
				command_Action.defaultLabel = "DEV: Set progress to 1";
				command_Action.action = delegate()
				{
					this.Progress = 1f;
				};
				yield return command_Action;
				command_Action = null;
			}
			bool flag3 = this.SpaceLeftForWort > 0;
			if (flag3)
			{
				Command_Action command_Action2 = new Command_Action();
				command_Action2.defaultLabel = "DEV: Fill";
				command_Action2.action = delegate()
				{
					this.Progress = 1f;
					this.wortCount = this.def.GetModExtension<Building_FermentationDefMod>().MaxCapacity;
				};
				yield return command_Action2;
				command_Action2 = null;
			}
			yield break;
		}

		// Token: 0x04000001 RID: 1
		private int wortCount;

		// Token: 0x04000002 RID: 2
		private float progressInt;

		// Token: 0x04000003 RID: 3
		private Material barFilledCachedMat;

		// Token: 0x04000004 RID: 4
		public const int MaxCapacity = 25;

		// Token: 0x04000005 RID: 5
		private const int BaseFermentationDuration = 360000;

		// Token: 0x04000006 RID: 6
		public const float MinIdealTemperature = 7f;

		// Token: 0x04000007 RID: 7
		private static readonly Vector2 BarSize = new Vector2(0.55f, 0.1f);

		// Token: 0x04000008 RID: 8
		private static readonly Color BarZeroProgressColor = new Color(0.4f, 0.27f, 0.22f);

		// Token: 0x04000009 RID: 9
		private static readonly Color BarFermentedColor = new Color(0.9f, 0.85f, 0.2f);

		// Token: 0x0400000A RID: 10
		private static readonly Material BarUnfilledMat = SolidColorMaterials.SimpleSolidColorMaterial(new Color(0.3f, 0.3f, 0.3f), false);
	}
}
